import {
    reqByAlarmId,
    reqWorkOrderUpdate,
    reqClosedLoop
} from "@api/api.js";

export default {
    data() {
        return {
            workStr: '',
            workList: [],
            addWork: {
                maintenanceUserId: "",
                remark: "",
                status: 2,
                userIds: []
            },
            statusList: [{
                    id: 2,
                    name: "接单",
                },
                {
                    id: 11,
                    name: "重新派单",
                },
            ],
            // 表单验证规则
            rulesWork: {
                status: [{
                    required: true,
                    message: "请选择",
                    trigger: "change",
                }, ],
            },
            picList: [],
            videoFile: '',
            fileList2: [],
            activeName: "first",
        };
    },
    computed: {
        getlabel() {
            if (this.workStr.status == 2) {
                return "是否接单";
            } else if (this.workStr.status == 3) {
                return "维修状态";
            } else if (this.workStr.status == 4 || this.workStr.status == 5) {
                return "审核结果";
            }
        },
    },
    components: {},
    mounted() {},
    methods: {
        handleClick(tab, event) {
            // console.log(tab.paneName);
            // if (tab.paneName == "first") {

            // } else if (tab.paneName == "second") {

            // } else if (tab.paneName == "third") {

            // }
        },
        // 处理工单
        addSubmitWork() {
            this.$refs.refaddWork.validate(async(valid) => {
                if (valid) {
                    let params = {}
                    if (this.addWork.status == 11) {
                        // 重新派单
                        params = {
                            id: this.workStr.id,
                            userId: this.user.userId,
                            username: this.user.userName,
                            status: this.addWork.status,
                            result: "1",
                            remark: this.addWork.remark,
                            maintenanceUserId: this.addWork.maintenanceUserId,
                            picList: [],
                            videoFile: '',
                            userIds: this.addWork.userIds || [],
                        }
                    } else {
                        params = {
                            id: this.workStr.id,
                            userId: this.user.userId,
                            username: this.user.userName,
                            status: this.addWork.status,
                            result: "1",
                            remark: this.addWork.remark,
                            maintenanceUserId: this.addWork.maintenanceUserId,
                            picList: this.picList,
                            videoFile: this.videoFile || '',
                            userIds: this.addWork.userIds || [],
                        }
                    }
                    const res = await reqWorkOrderUpdate(params);
                    if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
                    this.$modal.notifySuccess("处理成功！");

                    // 完结
                    if (this.addWork.status == 5) {
                        let res2 = await reqClosedLoop({
                            id: this.detailsInfo.id,
                        });
                        if (res2.code !== 20000) return this.$modal.notifyWarning(res2.message);
                    }

                    this.closeDialog();
                    setTimeout(() => {
                        this.$parent.AlarmHandleResult(); //更新告警列表
                    }, 100);
                }
            });
        },
        // 处理工单--------------------------
        // 根据告警id查询工单详情
        async getByAlarmId() {
            let res = await reqByAlarmId(this.detailsInfo.id);
            if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
            console.log(res, 222);
            this.workStr = res.data.row || ''
            this.workList = [];
            if (res.data.row != null) {
                if (res.data.row.workOrderLogList.length) {
                    res.data.row.workOrderLogList.forEach((item, index) => {
                        if (item.fileList) {
                            item.fileListArray = item.fileList.map(item2 => item2.file);
                        }
                        this.workList.unshift(item);
                    });
                }
                // 处理人自动选中
                this.addWork.userIds = []
                this.workList.forEach(item => {
                    this.userIds.forEach(item2 => {
                        if (item.username == item2.userName) {
                            this.addWork.userIds.push(item2.userId)
                        }
                    })
                });
                // console.log(this.workList, ' this.workList');

                if (this.workStr.status == 2) {
                    this.statusList = [{
                            id: 2,
                            name: "接单",
                        },
                        {
                            id: 11,
                            name: "重新派单",
                        },
                    ];
                    this.addWork.status = 2;
                }
                if (this.workStr.status == 3) {
                    this.statusList = [{
                            id: 4,
                            name: "修复中",
                        },
                        {
                            id: 5,
                            name: "修复完成",
                        },
                        {
                            id: 11,
                            name: "重新派单",
                        },
                    ];
                    this.addWork.status = 5;
                }
                if (this.workStr.status == 4) {
                    this.statusList = [{
                            id: 6,
                            name: "通过",
                        },
                        {
                            id: 7,
                            name: "不通过",
                        },
                    ];
                    this.addWork.status = 6;
                }
                if (this.workStr.status == 5) {
                    this.statusList = [{
                            id: 8,
                            name: "通过",
                        },
                        {
                            id: 9,
                            name: "不通过",
                        },
                    ];
                    this.addWork.status = 8;
                }
            }

        },
        // 文件状态改变时的钩子
        uploadSectionFile2(file, fileList) {
            if (fileList.length >= 5) {
                this.uploadDisabled = true; //隐藏加号
            }
        },
        //删除照片
        handleRemove2(file, fileList) {
            // console.log(fileList);
            this.picList = [];
            fileList.forEach((item) => {
                if (item.response) {
                    this.picList.push(item.response.data.url);
                } else {
                    this.picList.push(item.url);
                }
            });
            setTimeout(() => {
                this.uploadDisabled = false;
            }, 1000);
        },
        // 点击文件列表中已上传的文件时的钩子
        handlePictureCardPreview(file) {
            // console.log(file);
            this.dialogImageUrl = file.url;
            this.dialogVisible2 = true;
        },
        // 上传文件之前的钩子
        beforeAvatarUpload2(file) {
            let isJPG = true;
            if (file.type !== "image/png" && file.type !== "image/jpeg") {
                this.$message.error("图片只能是 jpg/png 格式!");
                isJPG = false;
            }
            let isLt = file.size / 1024 < 20000; // 判断是否小于500Kb  500
            if (!isLt) {
                this.$modal.notifyWarning("图片大小不能超过20MB! 请重新上传");
                this.uploadDisabled = false;
                isJPG = false;
            }
            return isJPG;
        },
        // 上传成功
        handleSuccessFunction2(response, file, fileList) {
            this.picList.push(file.response.data.url);
        },
        // 文件超出个数限制时的钩子
        handleExceedScene2() {
            this.$modal.notifyWarning(`照片最多6张`);
        },
        //上传失败
        uploadFileError(err, file, fileList) {
            this.uploadDisabled = false;
        },
        // 上传成功
        onsuccessVideo(response, file, fileList) {
            // console.log(file);
            this.videoFile = file.response.data.url;
        },
        //删除视频
        handleVideo(file, fileList) {
            this.videoFile = "";
        },
        handleExceed(files, fileList) {
            this.$modal.notifyWarning(`只能上传一个视频文件`);
        },
        // 视频
        beforeUploadVideo(file) {
            const isLt30M = file.size / 1024 / 1024 < 30;
            if (
                [
                    "video/mp4",
                    "video/ogg",
                    "video/flv",
                    "video/avi",
                    "video/wmv",
                    "video/rmvb",
                ].indexOf(file.type) == -1
            ) {
                this.$modal.notifyWarning("请上传正确的视频格式");
                return false;
            }
            if (!isLt30M) {
                this.$modal.notifyWarning("上传视频大小不能超过30MB哦!");
                return false;
            }
        },
    },
};